//
//  CoreEngine.m
//  BizLeader
//
//  Created by Draven on 2018/1/18.
//  Copyright © 2018年 biz. All rights reserved.
//

#import "CoreEngine.h"
#import "ConfigurationManager.h"

#define USER_ACCESS_TOKEN @"6de9cc05-6c46-4e29-84f3-a638e442465a"

@implementation CoreEngine
+ (instancetype)sharedInstance {
    static CoreEngine *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[CoreEngine alloc] initWithBaseURL:[NSURL URLWithString:[ConfigurationManager sharedInstance].url]];
    });
    return _sharedInstance;
}
- (instancetype)initWithBaseURL:(NSURL *)url {
    if (self = [super initWithBaseURL:url]) {
        
        /**分别设置请求以及相应的序列化器*/
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        /**设置请求超时时间*/
        self.requestSerializer.timeoutInterval = 60;
        /**设置相应的缓存策略*/
        self.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
        /**复杂的参数类型 需要使用json传值-设置请求内容的类型*/
        [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [self.requestSerializer setValue:USER_ACCESS_TOKEN forHTTPHeaderField:@"X-SS-USER-ACCESS-TOKEN"];

//        [self.requestSerializer setValue:url.absoluteString forHTTPHeaderField:@"Referer"];
//        self.securityPolicy.allowInvalidCertificates = YES;
        AFJSONResponseSerializer * response = [AFJSONResponseSerializer serializer];
        response.removesKeysWithNullValues = YES;
        self.responseSerializer = response;
        /**设置接受的类型*/
        [self.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/plain",@"application/json",@"text/json",@"text/javascript",@"text/html", nil, nil]];
        
    }
    return self;
}

+ (NSURLSessionDataTask *)requestWithRequestType:(HTTPSRequestType)type urlString:(NSString *)urlString paraments:(NSObject *)paraments completeBlock:(void (^)(id _Nullable, NSError * _Nullable))completeBlock {
    if (!urlString || urlString.length <= 0) {
        return nil;
    }
    switch (type) {
        case HTTPSRequestTypeGet:
        {
            //所有 Get 请求，增加缓存机制
            return [[CoreEngine sharedInstance] GET:urlString parameters:paraments progress:^(NSProgress * _Nonnull downloadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                completeBlock(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                completeBlock(nil,error);
            }];
            
        }
            break;
        case HTTPSRequestTypePost:
        {
            return [[CoreEngine sharedInstance] POST:urlString parameters:paraments progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                completeBlock(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                completeBlock(nil,error);
            }];
        }
            break;
        case HTTPSRequestTypePut:
        {
            return [[CoreEngine sharedInstance] PUT:urlString parameters:paraments success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                completeBlock(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                completeBlock(nil,error);
            }];
        }
            break;
        case HTTPSRequestTypeDelete:
        {
            return [[CoreEngine sharedInstance] DELETE:urlString parameters:paraments success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                completeBlock(responseObject,nil);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                completeBlock(nil,error);
            }];
        }
            break;
        default:
            break;
    }
}


@end
